package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.custom.uielement.decorate.IdentifierFactory;
import com.activenetwork.schema.testcase.events.createevents.basics.basics012.Basics012DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.DateUtil;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.datepicker.DatePicker;
import com.activenetwork.util.dropdownmenu.DropdownMenu;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.VerificationAction;
import com.activenetwork.util.verification.checkCalendarType;
import com.activenetwork.util.verification.checkDropdownType;
import com.activenetwork.util.verification.checkType;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics012 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics012.class);
	private EventBusiness eventBusiness = new EventBusiness();
	private Basics012DataType data;
	
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() {		
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(Basics012.class);
			data = helper.bind(Basics012DataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}
	
	
	@Test
	public void Check_one_day_in_Event_Length(){
		
		loginBusiness.login();
		
		// delete Basics012 event if exist
		try{
			eventBusiness.deleteEvent("basics012", data.getEvent());
		}catch(RuntimeException e){
			logger.info("No need to delete basics012 since not found");
		}
		
		EventBasicSubview eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		logger.info("Verify event length radio");
		new checkType("EventBasicSubview", "eventLengthSingleDay", "radio", Basics012.class.getName()).verify();
		new checkType("EventBasicSubview", "eventLengthMultiDay", "radio", Basics012.class.getName()).verify();
		
		logger.info("Verify default event length is multiple day");
		new VerificationAction(){

			@Override
			public void verify() {
				WebElement element = IdentifierFactory.createWebElement("EventBasicSubview", "eventLengthSingleDay");
				String checked = element.getAttribute("checked");
				if(checked != null){
					EnvContext.failurePointTL.get().add(
							new FailurePoint(Basics012.class.getName(), 
							"Verify multi-day default checked", 
							"Single day is checked by default"));
				}
			}
			
		}.verify();
		
		logger.info("Verify start/end date is calendar type");
		new checkCalendarType("EventBasicSubview", "eventStartDate", Basics012.class.getName());
		new checkCalendarType("EventBasicSubview", "eventEndDate", Basics012.class.getName());
		
		logger.info("Click one day radio");
		IdentifierFactory.createWebElement("EventBasicSubview", "eventLengthSingleDay").click();
		
		logger.info("Verify event date is calendar type");
		new checkCalendarType("EventBasicSubview", "eventDate", Basics012.class.getName());
		
		logger.info("Verify event date default value is empty");
		new VerificationAction(){

			@Override
			public void verify() {
				WebElement element = IdentifierFactory.createWebElement("EventBasicSubview", "eventDate");
				String value = element.getAttribute("value").trim();
				if(value != null && value.length() > 0){
					EnvContext.failurePointTL.get().add(
							new FailurePoint(Basics012.class.getName(), 
							"Verify one day event date default value", 
							"Event date default value is not empty"));
				}
			}
			
		}.verify();
		
		logger.info("Verify start/end time is dropdown type");
		new checkDropdownType("EventBasicSubview", "startTime", Basics012.class.getName()).verify();
		new checkDropdownType("EventBasicSubview", "endTime", Basics012.class.getName()).verify();
		
		logger.info("Verify event date is a date calendar and set up the date to be one day later");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, cal.get(Calendar.DATE)+1);
		DatePicker dataPicker = new DatePicker(IdentifierFactory.createWebElement("EventBasicSubview", "eventDate"));
		dataPicker.pick(cal);
		
		logger.info("Verify event date can not be earlier than system date");
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
		dataPicker = new DatePicker(IdentifierFactory.createWebElement("EventBasicSubview", "eventDate"));
		dataPicker.pick(cal);
		logger.warn("Currently, this verification is not implemented");
		
		logger.info("Select end time less than start time and verify the validation");
		eventBasicSubview.events();
		eventBusiness.addEvent(data.getEvent());
		
		List<WebElement> errors = webDriver.findElements(By.cssSelector(".nexus-errors div.alert.alert-error>div"));
		boolean found = false;
		for(WebElement error : errors){
			String content = error.getText().trim();
			if(content.contains("must less than")){
				found = true;
				break;
			}
		}
		if(!found){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics012.class.getName(), 
					"Verify one-day event start time should be earlier than end time", 
					"One-day event start time is later than end time"));
		}
		
		logger.info("Select end time equals to start time should fire validation");
		String option = DateUtil.toTimeOption(data.getEvent().getBasic().getLength().getOneDay().getStartTime());
		DropdownMenu endTimeDropdown = new DropdownMenu(IdentifierFactory.createWebElement("EventBasicSubview", "endTime"));
		endTimeDropdown.select(option);
		IdentifierFactory.createWebElement("EventBasicSubview", "sourcing").click();
		
		errors = webDriver.findElements(By.cssSelector(".nexus-errors div.alert.alert-error>div"));
		found = false;
		for(WebElement error : errors){
			String content = error.getText().trim();
			if(content.contains("must less than")){
				found = true;
				break;
			}
		}
		if(!found){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics012.class.getName(), 
					"Verify one-day event start time equals to end time", 
					"One-day event start time should not equal to end time"));
		}
		
		loginBusiness.logout();
		FailureAssertion.pop(Basics012.class);
	}
}
